/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JOptionPane;

/**
 *
 * @author OMADA 6
 * Basi endiktikis ergasias palioterou etous
 */
public class DBManager {
    private static EntityManagerFactory emf;
    private static  EntityManager em;
    //= 
      // javax.persistence.Persistence.createEntityManagerFactory("radioPU").createEntityManager();
    public static void openConnection() {
        // Xrisi try catch exception gia epikoinonia me tin bd 
        try {
            // Dimiourgia EntityManagerFactory gia to radioPU 
            emf = Persistence.createEntityManagerFactory("radioPU"); 
            // Dimioyrgia EntityManager 
            em = emf.createEntityManager(); 
        } catch(Exception e) {
            // Enimerosi me minima sfalmatos apotixias
            System.out.println(e); 
            JOptionPane.showMessageDialog(null, "Αποτυχία σύνδεσης με τη ΒΔ!",
                    "ERROR", JOptionPane.ERROR_MESSAGE);
            // Eksodos apo to programa an den exoyme basi 
            // System.exit(0);
        }
    }

    public static void closeConnection() {
        // Xrisi try catch exception gia epikoinonia me tin bd
        try {
        // Klisimo EntityManager 
            em.close();
        // klisimo EntityManagerFactory 
            emf.close();
        } catch(Exception e){ 
            System.out.println(e);
            JOptionPane.showMessageDialog(null, "Αποσύνδεση από τη ΒΔ!", "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    public static EntityManager getEm() {
        //Epistrefi ton em
        return em;
    }
}
